/*
 * UMLSynch plugin for Eclipse
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.eclipse.rpc;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.ui.PlatformUI;

import com.googlecode.umlsynch.EclipseOperationServer.VPSDEClassDescription;
import com.googlecode.umlsynch.dialogs.ListChecker2;
import com.googlecode.umlsynch.eclipse.astutils.ASTUtils;
import com.googlecode.umlsynch.eclipse.astutils.TranslationUnitUtils;
import com.googlecode.umlsynch.rpc.IIndexerRpc;
import com.googlecode.umlsynch.rpc.TClassDescriptor;
import com.googlecode.umlsynch.rpc.TFileDescriptor;
import com.googlecode.umlsynch.rpc.base.IDebug;
import com.googlecode.umlsynch.rpc.base.TDebug;

public class TIndexerRpcImplementation implements IIndexerRpc {

	private TDebug debug;
	private ASTUtils astutils = ASTUtils.instance();;
	private TranslationUnitUtils tuUtils = TranslationUnitUtils.GetInstance();

	public TIndexerRpcImplementation(IDebug dbg) {
		debug = new TDebug(dbg);
	}

	/* (non-Javadoc)
	 * @see com.googlecode.umlsynch.rpc.IIndexerRpc#GetClassInfo(com.googlecode.umlsynch.rpc.TClassDescriptor)
	 */
	@Override
	public TClassDescriptor GetClassInfo(TClassDescriptor desc) {

		// Steps to get class info :
		// 1) Check class name != null
		// 2) Check project != null
		// 3) Get Project Language 
		// 4) Redirect request for corresponding Indexer
		//    CDT, JDT or PyDev
		
		
		return desc;

	}

	/* (non-Javadoc)
	 * @see com.googlecode.umlsynch.rpc.IIndexerRpc#GetClassRelations(com.googlecode.umlsynch.rpc.TClassDescriptor[], com.googlecode.umlsynch.rpc.IIndexerRpc.TRelationRpc, com.googlecode.umlsynch.rpc.TFileDescriptor[])
	 * Steps for relation identification:
	 * 
	 * In UMLSynchExtensions:
	 * 1. Identify class
	 * 2. Identify relation type
	 * 3. Find all relations
	 * 4. Apply searchRestriction for results
	 * 
	 * In this class:
	 * 5. Open result-selection dialog 
	 *    (TODO: Add flag for API which enable/disable this dialog.
	 *    It is necessary for syntax-based patters etc)
	 */
	@Override
	public TClassDescriptor[] GetClassRelations(TClassDescriptor desc,
			TRelationRpc relType, TFileDescriptor[] searchRestrictions) {

		// Find class relations		
		TClassDescriptor[] result = null;
		
		if (result == null) {
			debug.Warning("There is no relation found for : " + desc.name);
			return null;
		}
		
		// Select the resulted methods, and add 
		// an identified class to the top of the results
		
		List<TClassDescriptor> resultList = new ArrayList<TClassDescriptor>();
		// add an updated class element
		resultList.add(desc);

		if ((result != null) && (result.length > 0)) {

			ListChecker2 resultsSelector = new ListChecker2();

			resultsSelector.filesList = new String[result.length];
			resultsSelector.namesList = new String[result.length];

			for (int i=0; i<result.length; ++i) {
				resultsSelector.namesList[i] = result[i].name;
				resultsSelector.filesList[i] = 
					(result[i].file.path == null) ? "unknown" : result[i].file.path;
			}

			PlatformUI.getWorkbench().getDisplay().syncExec(resultsSelector);

			if (resultsSelector.selNamesList != null)
				for (int i=0; i<result.length; ++i) {
					for (int j=0; j<resultsSelector.selNamesList.length; ++j) {
						if ((resultsSelector.selNamesList[j] == result[i].name)
							&& ((resultsSelector.selFilesList[j] == result[i].file.path) 
							|| ((resultsSelector.selFilesList[j] == "unknown") 
							&& (result[i].file.path == null)))){
							resultList.add(result[i]);
							break;
						}
					}
				}
		} else {
			debug.Note("Result of search is null");
		}
		return this.tuUtils.DynamcCastListToArray(resultList);
	}
}
